package com.venus.lms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.venus.lms.mapper.BookMapper;
import com.venus.lms.mapper.LendRecordMapper;
import com.venus.lms.mapper.UserMapper;
import com.venus.lms.pojo.Book;
import com.venus.lms.pojo.LendRecord;
import com.venus.lms.pojo.LoginUser;
import com.venus.lms.pojo.User;
import com.venus.lms.utils.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据可视化控制器
 *
 * @author DQM
 */
@RestController
@RequestMapping("/dashboard")
public class DashboardController {
    @Resource
    private UserMapper userMapper;
    @Resource
    private LendRecordMapper lendRecordMapper;
    @Resource
    private BookMapper bookMapper;

    @GetMapping
    public Result<?> dashboardrecords() {
        //访问数量
        int visitCount = LoginUser.getVisitCount();
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
        //用户数量
        int userCount = Math.toIntExact(userMapper.selectCount(queryWrapper1));
        QueryWrapper<LendRecord> queryWrapper2 = new QueryWrapper<>();
        //已借阅数量
        int lendRecordCount = Math.toIntExact(lendRecordMapper.selectCount(queryWrapper2));
        QueryWrapper<Book> queryWrapper3 = new QueryWrapper<>();
        //图书数量
        int bookCount = Math.toIntExact(bookMapper.selectCount(queryWrapper3));
        Map<String, Object> map = new HashMap<>();
        //键值对形式存入map
        map.put("visitCount", visitCount);
        map.put("userCount", userCount);
        map.put("lendRecordCount", lendRecordCount);
        map.put("bookCount", bookCount);
        return Result.success(map);
    }
}
